#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <fstream>
#include <QDebug>
#include <QTextEdit>
#include <QFileDialog>
#include <QToolBar>
#include <QStatusBar>
#include <QLabel>

#include "dialog.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //菜单栏
    this->setWindowTitle("my text");
    QMenuBar* menuBar = this->menuBar();
    QMenu* fileMenu = new QMenu("File&(f)");
    menuBar->addMenu(fileMenu);
    QAction* openAction = new QAction("open&(o)");
    QAction* saveAction = new QAction("save&(s)");
    fileMenu->addAction(openAction);
    fileMenu->addAction(saveAction);

    //给两个Action绑定槽函数
    openAction->connect(openAction, &QAction::triggered,this, &MainWindow::OpenFileAction);
    openAction->connect(saveAction, &QAction::triggered,this, &MainWindow::SaveFileAction);

    //TextEdit作为“编辑器,”若要自适应变化，需要处理resizeEvent事件
    ui->editor->setGeometry(this->geometry());

    //工具栏
    QToolBar* toolBar = new QToolBar(this);
    this->addToolBar(Qt::ToolBarArea::TopToolBarArea, toolBar);
    toolBar->addAction(openAction);
    toolBar->addAction(saveAction);

    //状态栏
    QStatusBar* statusBar = this->statusBar();
    QLabel* message = new QLabel(this);
    message->setText("MESSAGE:");
    statusBar->addWidget(message);
    this->setStatusBar(statusBar);

    //对话框
    Dialog* dialog = new Dialog(this);
    dialog->show();
    dialog->setAttribute(Qt::WA_DeleteOnClose);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//打开时，调用show打开的就是Qt内置的窗口；否则打开当前操作系统对应的窗口
void MainWindow::OpenFileAction()
{
    QFileDialog* dialog = new QFileDialog(this);
    dialog->setAcceptMode(QFileDialog::AcceptOpen);
    //dialog->show();
    qDebug() << "open";

    QString filename = QFileDialog::getOpenFileName();
    qDebug() << filename;

    std::ifstream ifs(filename.toStdString());
    if(!ifs.is_open())
    {
        ifs.close();
        return;
    }

    //若一行一行地读取，需要在源文本内容基础上加上读取到的内容,开销有点大？
//    std::string data;
//    while(getline(ifs, data))
//    {
//        qDebug() << QString::fromStdString(data);
//        ui->editor->setText(ui->editor->toPlainText()+ QString::fromStdString(data));
//    }

    // get length of file:
    ifs.seekg (0, ifs.end);
    int length = ifs.tellg();
    ifs.seekg (0, ifs.beg);

    // allocate memory:
    char * buffer = new char [length];
    ifs.read(buffer, length);

    ui->editor->setText(QString(buffer));
    delete [] buffer;

    ifs.close();
    dialog->setAttribute(Qt::WA_DeleteOnClose);     //Qt内置的处理对话框内存泄漏问题的方法
}

//保存时，调用show打开的就是Qt内置的窗口；否则打开当前操作系统对应的窗口
void MainWindow::SaveFileAction()
{
    QFileDialog* dialog = new QFileDialog(this);
    dialog->setAcceptMode(QFileDialog::AcceptSave);
    qDebug() << "save";
    //dialog->show();

    QString filename = QFileDialog::getSaveFileName();
    qDebug() << filename;

    std::ofstream ofs(filename.toStdString());
    std::string outData = ui->editor->toPlainText().toStdString();
    ofs.write(outData.c_str(), outData.size());

    ofs.close();
    dialog->setAttribute(Qt::WA_DeleteOnClose);
}

